<template>
  <div class="content">
    <div>
      <MySearch :form-inline="formInline" @searchFn="searchFn">
        <template>
          <el-col :span="8">
            <el-form-item label="货主编号" prop="code">
              <el-input v-model="formInline.code" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="货主名称" prop="name">
              <el-input v-model="formInline.name" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="personName">
              <el-input v-model="formInline.personName" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </template>
      </MySearch>
    </div>
    <div class="table">
      <div>
        <MyTable :table-data="locationList" :table-col="tableCol" :current="current" :size="size">
          <template #btn>
            <div style="display:flex;justify-content: space-between;">
              <el-button class="add" @click="$router.push({ name: 'goods-owner-detile', params: { id: 'null' } })">新增货主
              </el-button>
            </div>
          </template>
          <template #edit="props">
            <el-button
              type="text"
              size="small"
              @click="$router.push({ name: 'owner-detile', params: { id: props.status.id } })"
            >查看详情</el-button>
            <el-button
              type="text"
              size="small"
              @click="$router.push({ name: 'goods-owner-detile', params: { id: props.status.id } })"
            >编辑</el-button>
            <el-button type="text" size="small" @click="delFn(props.status)">删除</el-button>
          </template>
        </MyTable>
      </div>
      <div>
        <MyPage :total-page="totalPage" @changeSize="changeSize" @changeCurrent="changeCurrent" />
      </div>
    </div>
  </div>
</template>

<script>
import MySearch from '@/components/MySearch'
import MyTable from '@/components/MyTable'
import MyPage from '@/components/MyPage'
import { ownerPage, delOwner } from '@/api'
export default {
  components: { MySearch, MyTable, MyPage },
  data() {
    return {
      formInline: {},
      locationList: [],
      tableCol: [
        { prop: 'code', label: '货主编号', width: '160px' },
        { prop: 'name', label: '货主名称', width: '160px' },
        { prop: 'personName', label: '联系人', width: '160px' },
        { prop: 'phone', label: '联系电话', width: '160px' },
        { prop: 'email', label: '联系邮箱', width: '280px' },
        { prop: 'location', label: '省/市/区', width: '280px' },
        { prop: 'address', label: '详细地址', width: '350px' },
        { prop: 'updateTime', label: '更新时间', width: '200px' }
      ],
      totalPage: null,
      size: 10,
      current: 1,
      dialogVisible: false
    }
  },
  async created() {
    await this.warehouseFn(this.formInline, this.size, this.current)
  },
  methods: {
    // 请求
    async warehouseFn(obj, size, current) {
      const res = await ownerPage({
        ...obj,
        current,
        size,
        descs: 'createTime'
      })
      this.totalPage = Number(res.data.data.total)
      this.locationList = res.data.data.records
      return res.data
    },
    // 搜索
    async searchFn() {
      const res = await this.warehouseFn(this.formInline, this.size, this.current)
      console.log(res)
    },
    // 改变页数
    async changeSize(val) {
      this.size = val
      await this.warehouseFn(this.formInline, this.size, this.current)
    },
    async changeCurrent(val) {
      this.current = val
      await this.warehouseFn(this.formInline, this.size, this.current)
    },
    async delFn(data) {
      this.$confirm(`确定要删除吗？`, `提示`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delOwner(data.id).then(() => {
          this.$message({
            type: 'success',
            message: `删除成功!`
          })
        }).catch(res => {
          console.log(res.response)
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
        await this.warehouseFn(this.formInline, this.size, this.current)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: `已取消删除`
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/my-form.scss';
@import '@/styles/common.scss';

</style>
